const form = document.querySelector('form')
const nameInp = document.querySelector('.username')
// console.log(nameInp)pa
const pwdInp = document.querySelector('.password')
const rpwdInp = document.querySelector('.rpassword')
const nickInp = document.querySelector('.nickname')
const errBox = document.querySelectorAll('.error')
console.log(form,nameInp,pwdInp,rpwdInp,nickInp,errBox)

// 表单提交事件
form.addEventListener('submit', e => {
    e.preventDefault()

    // 采集用户信息
    const info = {
        username: nameInp.value,
        password: pwdInp.value,
        rpassword: rpwdInp.value,
        nickname: nickInp.value
    }
    // console.log(info)

    // 非空验证
    if (!info.username || !info.password || !info.rpassword || !info.nickname) {
        for (var i = 0; i < errBox.length; i++) {
            errBox[i].style.display = "block";
        }
        return
    }
    if (info.password !== info.rpassword) return alert('两次密码不一样')

    ajax({
        url: 'http://localhost:8888/users/register',
        method: 'POST',
        data: info,
        dataType: 'json',
        success(res) {
            // res 就是后端返回的结果
            if (res.code === 0) {
                // errBox.style.display = 'block'
                return alert('注册失败')
            } else {
                for (var i = 0; i < errBox.length; i++) {
                    errBox[i].style.display = "none";
                }
                alert('注册成功, 点击确定跳转到登录页')
                window.location.href = '../views/login.html'
            }
        }
    })
})